package com.service.impl;

import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.entity.Admin;
import com.mapper.UserMapper;
import com.untils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.User;
import com.mapper.AdminMapper;
import com.service.AdminService;
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService{
    @Autowired
    AdminMapper adminMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public String login(Admin admin) {
        System.out.println("进入login");
        List<Admin> selectedList = list(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getUser, admin.getUser()));
        if (selectedList.isEmpty()) {
            throw new RuntimeException("账号不存在");
        }
        Admin selected = selectedList.get(0);
        if(!admin.getPsw().equals(selected.getPsw())){
            throw new RuntimeException("密码错误");
        }
        // 生成令牌
        HashMap<String, Object> map = new HashMap<>(2);
        map.put("mobile", selected.getUser());
        System.out.println("返回令牌，令牌为：" + JwtUtil.generateToken(map));
        return JwtUtil.generateToken(map);
    }

    @Override
    public int deleteUser(User user) {
        return userMapper.deleteById(user);
    }

    @Override
    public int upDateUser(User user) {
        System.out.println("upDateUser:"+user);
        return userMapper.updateById(user);
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.selectList(null);
    }
    
}
